﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

class Segment
{
    public PointF p;
    public PointF q;
    public float m, b;
    public Segment(PointF p1, PointF p2)
    {
        p = p1;
        q = p2;
        m = (p.Y - q.Y) / (p.X - q.X);
        b = p.Y - m * p.X;  
        //Console.WriteLine(m + " and " + b);
    }
    public Boolean hasPoint(PointF pt)
    {
        //return this.b == pt.Y - this.m * pt.X;
      //  if (pt.Y > q.Y || pt.Y > p.Y || pt.X > q.X || pt.X > pt.X) return false;
        //if (pt.Y < q.Y || pt.Y < p.Y || pt.X < q.X || pt.X < pt.X) return false;
        //return (pt.Y - p.Y) / (pt.X - p.X)
          //  == (q.Y - p.Y) / (q.X - p.X);

        //return this.b == pt.Y - this.m * pt.X;
        //if (pt.Y > q.Y || pt.Y > p.Y || pt.X > q.X || pt.X > pt.X) return false;
        //if (pt.Y < q.Y || pt.Y < p.Y || pt.X < q.X || pt.X < pt.X) return false;
        //return (pt.Y - p.Y) / (pt.X - p.X) == (q.Y - p.Y) / (q.X - p.X);

        float dx1 = p.X - pt.X; //the horizontal distance from pt to p
        float dy1 = p.Y - pt.Y; //the vertical distance from pt to p
        float dx2 = q.X - pt.X; //the horizontal distance from pt to q
        float dy2 = q.Y - pt.Y; //the vertical distance from pt to q

        if (dx1 * dx2 < 0 || dy1 * dy2 < 0) //elimates the case when pt is on the extended line of seg pq
            return (((q.X - p.X) / (q.Y - p.Y) * (pt.Y - p.Y) + p.X - pt.X) <= 1
            && ((q.X - p.X) / (q.Y - p.Y) * (pt.Y - p.Y) + p.X - pt.X) >= -1); //allows a range of error of 1 pixel
        return (dx1 == 0 && dy1 == 0) || (dx2 == 0 && dy2 == 0);
    }
    public bool contains(SuperPoint point)
    {
        if (p.Equals(point.P) || q.Equals(point.P))
            return true;
        return false;
    }


}
